//
//  ChangeAppIconManager.swift
//  ChangeAppIcon
//
//  Created by 波波 on 2019/4/29.
//  Copyright © 2019 波波. All rights reserved.
//

import UIKit

class ChangeAppIconManager {
    
    /// 是否支持更换app icon
    /// iOS 10.3开始提供的更换app icon的API
    /// - Returns: true（支持） false（不支持）
    static func isSupportChangeAppIcon() -> Bool {
        if #available(iOS 10.3, *) {
            return UIApplication.shared.supportsAlternateIcons
        } else {
            // Fallback on earlier versions
            return false
        }
    }
    
    /// 更换app icon
    ///
    /// - Parameter iconName: iconName 
    static func changeAppIcon(_ iconName: String?) {
        if #available(iOS 10.3, *) {
            if UIApplication.shared.supportsAlternateIcons {
                UIApplication.shared.setAlternateIconName(iconName) { (error: Error?) in
                    if let error = error {
                        print("更换app icon发生错误\(error)")
                    }
                }
            } else {
                let alertVC = UIAlertController.init(title: "APP暂不支持此功能，敬请期待", message: nil, preferredStyle: .alert)
                let sureAction = UIAlertAction.init(title: "确定", style: .default, handler: nil)
                alertVC.addAction(sureAction)
                let appDelegate = UIApplication.shared.delegate as! AppDelegate
                appDelegate.window?.rootViewController?.present(alertVC, animated: true, completion: nil)
            }
        } else {
            // Fallback on earlier versions
            let alertVC = UIAlertController.init(title: "您的系统版本不支持此功能，请升级系统", message: nil, preferredStyle: .alert)
            let sureAction = UIAlertAction.init(title: "确定", style: .default, handler: nil)
            alertVC.addAction(sureAction)
            let appDelegate = UIApplication.shared.delegate as! AppDelegate
            appDelegate.window?.rootViewController?.present(alertVC, animated: true, completion: nil)
        }
    }
}
